package com.handinfo.android.ui;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWBitmap;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.object.Animation;
import com.handinfo.android.core.resource.DWSerializableFactory;
import com.handinfo.android.core.resource.ResHead;
import com.handinfo.android.core.resource.ResourceManager;
import com.handinfo.android.ui.window.UIAchievement;
import com.handinfo.android.ui.window.UIAnnouncement;
import com.handinfo.android.ui.window.UIAnswer;
import com.handinfo.android.ui.window.UIAssistant;
import com.handinfo.android.ui.window.UIAthletics;
import com.handinfo.android.ui.window.UIBag;
import com.handinfo.android.ui.window.UIBagualu;
import com.handinfo.android.ui.window.UIBaitan;
import com.handinfo.android.ui.window.UIBattlefield;
import com.handinfo.android.ui.window.UIBlackList;
import com.handinfo.android.ui.window.UICheckPeople;
import com.handinfo.android.ui.window.UIExchangWindow;
import com.handinfo.android.ui.window.UIFamilyCarbonWindow;
import com.handinfo.android.ui.window.UIFamilyWindow;
import com.handinfo.android.ui.window.UIFirstRecharge;
import com.handinfo.android.ui.window.UIFixWindow;
import com.handinfo.android.ui.window.UIFriend;
import com.handinfo.android.ui.window.UIGameCarbonWindow;
import com.handinfo.android.ui.window.UIGetMoney;
import com.handinfo.android.ui.window.UIGiftBag;
import com.handinfo.android.ui.window.UIHelp;
import com.handinfo.android.ui.window.UIHelpGong;
import com.handinfo.android.ui.window.UIHook;
import com.handinfo.android.ui.window.UIJiMingTai;
import com.handinfo.android.ui.window.UILiaoTianWindow;
import com.handinfo.android.ui.window.UILuckDraw;
import com.handinfo.android.ui.window.UIMail;
import com.handinfo.android.ui.window.UIMain;
import com.handinfo.android.ui.window.UIMap;
import com.handinfo.android.ui.window.UIMarriage;
import com.handinfo.android.ui.window.UIMeri;
import com.handinfo.android.ui.window.UINPCtongji;
import com.handinfo.android.ui.window.UINeiDanWindow;
import com.handinfo.android.ui.window.UINpcShopWindow;
import com.handinfo.android.ui.window.UINrearby;
import com.handinfo.android.ui.window.UIPet;
import com.handinfo.android.ui.window.UIPetMenu;
import com.handinfo.android.ui.window.UIPrizeWindow;
import com.handinfo.android.ui.window.UIRankWindow;
import com.handinfo.android.ui.window.UIRecharge;
import com.handinfo.android.ui.window.UIRecord;
import com.handinfo.android.ui.window.UIReliveWindow;
import com.handinfo.android.ui.window.UIRenWuWindow;
import com.handinfo.android.ui.window.UIRoleStatsWindow;
import com.handinfo.android.ui.window.UISetSkillWindow;
import com.handinfo.android.ui.window.UISetup;
import com.handinfo.android.ui.window.UIShop;
import com.handinfo.android.ui.window.UIShopBuy;
import com.handinfo.android.ui.window.UIShuraBattlefield;
import com.handinfo.android.ui.window.UISocial;
import com.handinfo.android.ui.window.UISoulWindow;
import com.handinfo.android.ui.window.UITianGong;
import com.handinfo.android.ui.window.UITowerCarbon;
import com.handinfo.android.ui.window.UITuiJian;
import com.handinfo.android.ui.window.UITyfon;
import com.handinfo.android.ui.window.UIVip;
import com.handinfo.android.ui.window.UIWarrantWindow;
import com.handinfo.android.ui.window.UIWarehouse;
import com.handinfo.android.ui.window.UILetou;
import com.handinfo.android.ui.window.UIZhanLiAnim;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

public class UIWindows
{

	// private static Resource m_windows_resource = new Resource();

	private static UIWindows m_wins = null;

	public UIMain m_main = null;// 主界面
	public UIShop m_shop = null;// 商城
	public UIGetMoney m_getMoney = null;// 摇钱树
	public UISocial m_social = null;// 社交
	public UIFriend m_friend = null;// 好友
	public UIRenWuWindow m_renwu = null;// 任务
	public UILiaoTianWindow m_liaotian = null;// 聊天
	public UIMail m_mail = null;// 邮件
	public UINeiDanWindow m_neidan = null;// 内丹
	public UIGameCarbonWindow m_fuben = null;// 副本
	// public UIGongLueWindow m_gonglue = null;//攻略
	public UISetup m_setup = null; // 设置
	public UIJiMingTai m_jiming = null;// 祭命台
	public UITianGong m_tiangong = null;// 天工坊
	public UIGiftBag m_giftBag = null;// 礼包兑换
	public UIHelp m_help = null;// 游戏帮助
	public UIBag m_bag = null; // 背包
	public UIRoleStatsWindow m_rolestats = null; // 人物属性
	public UIAnnouncement m_announ = null;// 公告
	public UISetSkillWindow m_skill = null;// 技能
	public UIFamilyWindow m_family = null;// 家族
	public UINpcShopWindow m_npcShop = null; // 普通商店
	public UIReliveWindow m_relive = null;// 复活
	public UIExchangWindow m_exchang = null;// 交易
	public UIFixWindow m_fix = null;// 修理
	public UIMarriage m_marriage = null;// 婚姻
	public UIWarehouse m_warehouse = null;// 仓库
	public UILuckDraw m_luckdraw = null;// 抽奖
	public UIFirstRecharge m_firstRecharge = null;//首充
	public UIWarrantWindow m_warrant = null;// 通缉令
	public UISoulWindow m_soul = null;// 炼魂
	public UINPCtongji m_npctongji = null;// 查看通缉令

	public UICheckPeople m_checked = null;
	public UIVip m_vip = null;
	public UIPet m_pet = null;// 宠物
	public UIPetMenu m_petMenu = null;// 宠物菜单
	public UIBlackList m_blackList = null;// 黑名单
	public UIRecharge m_recharge = null;// 充值
	public UIRecord m_record = null;// 记录
	public UIShopBuy m_shopBuy = null;// 购买
	public UIHook m_hook = null;// 自动挂机
	public UIRankWindow m_rank = null;// 排行榜
	// public UIArena arena=null;
	public UIAthletics m_jingji = null;
	public Bitmap[] m_bagua = null;
	public UIMap m_map = null;// 世界地图
	public UITyfon m_tyfon = null;
	public UIAssistant m_assistant = null;
	public UINrearby m_nearby = null;// 附近玩家
	public UIFamilyCarbonWindow m_facarbon = null;
	public UIPrizeWindow m_prize = null;
	public UIBattlefield m_battlefield = null;// 战场
	public UIAnswer m_answer = null;// /答题
	public UIMeri m_meiri = null;// 每日
	public UIBagualu m_bagualu = null;// 每日
	public UITowerCarbon m_tower = null;// 塔式副本
	public UIBaitan m_baitan = null;// 摆摊
	public UIHelpGong m_help_gong = null;
	public UIShuraBattlefield m_shura_battle = null;// 摆摊
	public UILetou m_letou = null;// 乐透
	public UIAchievement m_achie = null;// 乐透
	public UITuiJian m_tuijian = null;//推荐集合
	public UIZhanLiAnim m_zhanli = null;//战力动画

	public static void createInstance()
	{

		if (m_wins == null)
			m_wins = new UIWindows();

		// 游戏帮助
		if (m_wins.m_help == null) {
			m_wins.m_help = new UIHelp();
			m_wins.m_help.init();
		}
		// 设置

		if (m_wins.m_setup == null) {
			m_wins.m_setup = new UISetup();
			m_wins.m_setup.init();
		}

		if (m_wins.m_bagua == null) {
			m_wins.m_bagua = new Bitmap[8];
			for (int i = 0; i < m_wins.m_bagua.length; i++) {
				m_wins.m_bagua[i] = UIWindows.createImage("/img/newui/bagua_"
						+ i + ".gnp");
			}
		}

	}

	public void drawBaGua(DWGraphics g)
	{
		DWGameManager.getInstance().getSendMessage().networkTimeLogic();
		if (m_bagua != null
				&& !DWGameManager.getInstance().getSendMessage()
						.ishaveNetLocked()) {
			g.setClip(0, 0, DWGameManager.Screen_Width,
					DWGameManager.Screen_Height);
			Bitmap img = m_bagua[m_timer % m_bagua.length];
			int img_w = img.getWidth();
			int img_h = img.getHeight();
			g.drawBitmap(m_bagua[m_timer % m_bagua.length],
					DWGameManager.Screen_Width - img_w, img_h, 0);
		}
	}

	public void init()
	{
		// 乐透
		if (m_letou == null) {
			m_letou = new UILetou();
			m_letou.init();
		}
		// 炼魂
		if (m_wins.m_soul == null) {
			m_wins.m_soul = new UISoulWindow();
			m_wins.m_soul.init();
		}
		// 摆摊
		if (m_baitan == null) {
			m_baitan = new UIBaitan();
			m_baitan.init();
		}
		// 每日
		if (m_meiri == null) {
			m_meiri = new UIMeri();
			m_meiri.init();
		}
		// NPC查看通缉
		if (m_wins.m_npctongji == null) {
			m_wins.m_npctongji = new UINPCtongji();
			m_wins.m_npctongji.init();
		}
		// 答题
		if (m_wins.m_answer == null) {
			m_wins.m_answer = new UIAnswer();
			m_wins.m_answer.init();
		}
		// 战场
		if (m_wins.m_battlefield == null) {
			m_wins.m_battlefield = new UIBattlefield();
			m_wins.m_battlefield.init();
		}
		// 聊天
		if (m_wins.m_liaotian == null) {
			m_wins.m_liaotian = new UILiaoTianWindow();
			m_wins.m_liaotian.init();
		}

		if (m_wins.m_renwu == null) {
			m_wins.m_renwu = new UIRenWuWindow();
			m_wins.m_renwu.init();
		}

		// //十字键
		// m_wins.m_taskTips = new UITaskTips();
		// m_wins.m_taskTips.init();
		//

		// 天工坊
		if (m_wins.m_tiangong == null) {
			m_wins.m_tiangong = new UITianGong();
			m_wins.m_tiangong.init();
		}

		// 祭命台
		if (m_wins.m_jiming == null) {
			m_wins.m_jiming = new UIJiMingTai();
			m_wins.m_jiming.init();
		}

		if (m_wins.m_giftBag == null) {
			m_wins.m_giftBag = new UIGiftBag();
			m_wins.m_giftBag.init();
		}

		// 主界面
		if (m_wins.m_main == null) {
			m_wins.m_main = new UIMain();
			m_wins.m_main.init();
		}
		// 商城
		if (m_wins.m_shop == null) {
			m_wins.m_shop = new UIShop();
			m_wins.m_shop.init();
		}
		// 购买
		if (m_wins.m_shopBuy == null) {
			m_wins.m_shopBuy = new UIShopBuy();
			m_wins.m_shopBuy.init();
		}
		// 摇钱树
		if (m_wins.m_getMoney == null) {
			m_wins.m_getMoney = new UIGetMoney();
			m_wins.m_getMoney.init();
		}
		// 社交
		if (m_wins.m_social == null) {
			m_wins.m_social = new UISocial();
			m_wins.m_social.init();
		}

	}

	public void init2()
	{
		// 好友
		if (m_wins.m_friend == null) {
			m_wins.m_friend = new UIFriend();
			m_wins.m_friend.init();
		}

		// 黑名单
		if (m_wins.m_blackList == null) {
			m_wins.m_blackList = new UIBlackList();
			m_wins.m_blackList.init();
		}

		// 兑换帮贡
		if (m_wins.m_help_gong == null) {
			m_wins.m_help_gong = new UIHelpGong();
			m_wins.m_help_gong.init();
		}

		// /个人竞技
		if (m_wins.m_jingji == null) {
			m_wins.m_jingji = new UIAthletics();
			m_wins.m_jingji.init();
		}
		// 任务
		if (m_wins.m_renwu == null) {
			m_wins.m_renwu = new UIRenWuWindow();
			m_wins.m_renwu.init();
		}

		// 副本
		if (m_wins.m_fuben == null) {
			m_wins.m_fuben = new UIGameCarbonWindow();
			m_wins.m_fuben.init();
		}
		// 修罗杀场
		if (m_wins.m_shura_battle == null) {
			m_wins.m_shura_battle = new UIShuraBattlefield();
			m_wins.m_shura_battle.init();
		}
		// 家族副本
		if (m_wins.m_facarbon == null) {
			m_wins.m_facarbon = new UIFamilyCarbonWindow();
			m_wins.m_facarbon.init();
		}
		// 邮件
		if (m_wins.m_mail == null) {
			m_wins.m_mail = new UIMail();
			m_wins.m_mail.init();
		}
		// 内丹
		if (m_wins.m_neidan == null) {
			m_wins.m_neidan = new UINeiDanWindow();
			m_wins.m_neidan.init();
		}

		// /助手
		if (m_wins.m_assistant == null) {
			m_wins.m_assistant = new UIAssistant();
			m_wins.m_assistant.init();
		}
		// /排行榜
		if (m_wins.m_rank == null) {
			m_wins.m_rank = new UIRankWindow();
			m_wins.m_rank.init();
		}

		// 背包
		if (m_wins.m_bag == null) {
			m_wins.m_bag = new UIBag();
			m_wins.m_bag.init();
		}
		// 人物属性
		if (m_wins.m_rolestats == null) {
			m_wins.m_rolestats = new UIRoleStatsWindow();
			m_wins.m_rolestats.init();
		}
		// 家族
		if (m_wins.m_family == null) {
			m_wins.m_family = new UIFamilyWindow();
			m_wins.m_family.init();
		}

		// 公告
		if (m_wins.m_announ == null) {
			m_wins.m_announ = new UIAnnouncement();
			m_wins.m_announ.init();
		}
		// 技能
		if (m_wins.m_skill == null) {
			m_wins.m_skill = new UISetSkillWindow();
			m_wins.m_skill.init();
		}

		// 普通商店
		if (m_wins.m_npcShop == null) {
			m_wins.m_npcShop = new UINpcShopWindow();
			m_wins.m_npcShop.init();
		}

		// 复活
		if (m_wins.m_relive == null) {
			m_wins.m_relive = new UIReliveWindow();
			m_wins.m_relive.init();
		}

		// 交易
		if (m_wins.m_exchang == null) {
			m_wins.m_exchang = new UIExchangWindow();
			m_wins.m_exchang.init();
		}

		// 修理
		if (m_wins.m_fix == null) {
			m_wins.m_fix = new UIFixWindow();
			m_wins.m_fix.init();
		}

	}

	public void init3()
	{
		// 婚姻
		if (m_wins.m_marriage == null) {
			m_wins.m_marriage = new UIMarriage();
			m_wins.m_marriage.init();
		}

		// 仓库
		if (m_wins.m_warehouse == null) {
			m_wins.m_warehouse = new UIWarehouse();
			m_wins.m_warehouse.init();
		}

		// 抽奖
		if (m_wins.m_luckdraw == null) {
			m_wins.m_luckdraw = new UILuckDraw();
			m_wins.m_luckdraw.init();
		}
		// 通缉令
		if (m_wins.m_warrant == null) {
			m_wins.m_warrant = new UIWarrantWindow();
			m_wins.m_warrant.init();
		}

		// 被选中人物菜单
		if (m_wins.m_checked == null) {
			m_wins.m_checked = new UICheckPeople();
			m_wins.m_checked.init();
		}

		// vip
		if (m_wins.m_vip == null) {
			m_wins.m_vip = new UIVip();
			m_wins.m_vip.init();
		}
		// 充值
		if (m_wins.m_recharge == null) {
			m_wins.m_recharge = new UIRecharge();
			m_wins.m_recharge.init();
		}
		// 记录
		if (m_wins.m_record == null) {
			m_wins.m_record = new UIRecord();
			m_wins.m_record.init();
		}

		// 宠物
		if (m_wins.m_pet == null) {
			m_wins.m_pet = new UIPet();
			m_wins.m_pet.init();
		}

		// 宠物菜单
		if (m_wins.m_petMenu == null) {
			m_wins.m_petMenu = new UIPetMenu();
			m_wins.m_petMenu.init();
		}

		// 世界地图
		if (m_wins.m_map == null) {
			m_wins.m_map = new UIMap();
			m_wins.m_map.init();
		}

		// //大喇叭喊话
		if (m_wins.m_tyfon == null) {
			m_wins.m_tyfon = new UITyfon();
			m_wins.m_tyfon.init();
		}
		// 自动挂机
		if (m_hook == null) {
			m_wins.m_hook = new UIHook();
			m_wins.m_hook.init();
		}
		// 塔式副本
		if (m_tower == null) {
			m_wins.m_tower = new UITowerCarbon();
			m_wins.m_tower.init();
		}
		// 附近玩家
		if (m_nearby == null) {
			m_wins.m_nearby = new UINrearby();
			m_wins.m_nearby.init();
		}
		if (m_answer == null) {
			m_wins.m_answer = new UIAnswer();
			m_wins.m_answer.init();
		}
		if (m_prize == null) {
			m_wins.m_prize = new UIPrizeWindow();
			m_wins.m_prize.init();
		}
		if (m_bagualu == null) {
			m_wins.m_bagualu = new UIBagualu();
			m_wins.m_bagualu.init();
		}
	}

	//新功能在这里添加
	public void init4()
	{
		if (m_achie == null) {//成就
			m_wins.m_achie = new UIAchievement();
			m_wins.m_achie.init();
		}
		if (m_wins.m_tuijian == null) {
			m_wins.m_tuijian = new UITuiJian();
			m_wins.m_tuijian.init();
		}

		if (m_wins.m_firstRecharge == null) {
			m_wins.m_firstRecharge = new UIFirstRecharge();
			m_wins.m_firstRecharge.init();
		}
		if (m_wins.m_zhanli == null) {
			m_wins.m_zhanli = new UIZhanLiAnim();
			m_wins.m_zhanli.init();
		}
	}

	public void initend()
	{
		if (DWControlsManager.getInstance().img_biaoqing == null) {
			DWControlsManager.getInstance().img_biaoqing = new Bitmap[26];
			for (int i = 0; i < 18; i++) {
				DWControlsManager.getInstance().img_biaoqing[i] = createImage("/img/icon/big_datubiao_"
						+ (i + 271) + ".gnp");
			}
			DWControlsManager.getInstance().img_biaoqing[18] = createImage("/img/newui/tianji_1.gnp");
			DWControlsManager.getInstance().img_biaoqing[19] = createImage("/img/newui/lingyin_1.gnp");

			DWControlsManager.getInstance().img_biaoqing[20] = createImage("/img/newui/vip_1.gnp");
			DWControlsManager.getInstance().img_biaoqing[21] = createImage("/img/newui/vip_2.gnp");
			DWControlsManager.getInstance().img_biaoqing[22] = createImage("/img/newui/vip_3.gnp");
			DWControlsManager.getInstance().img_biaoqing[23] = createImage("/img/newui/vip_4.gnp");
			DWControlsManager.getInstance().img_biaoqing[24] = createImage("/img/newui/vip_5.gnp");
			DWControlsManager.getInstance().img_biaoqing[25] = createImage("/img/newui/vip_6.gnp");
		}
		if (DWControlsManager.getInstance() != null) {
			DWControlsManager.getInstance().init();
		}
	}

	public void logic()
	{
		if (DWGameManager.getInstance().getGameState() == DWGameManager.GS_GAME) {
			// 答题
			if (m_wins.m_answer != null) {
				m_wins.m_answer.logic();
			}
			if (m_wins.m_social != null) {
				m_wins.m_social.logic();
			}
			if (m_wins.m_main != null) {
				m_wins.m_main.logic();
				// m_wins.m_main.liaotian();
			}
			if (m_wins.m_liaotian != null) {
				m_wins.m_liaotian.logic();
			}
			if (m_wins.m_jingji != null) {
				m_wins.m_jingji.logic();
			}
			if (m_wins.m_map != null) {
				m_wins.m_map.logic();
			}
			if (m_wins.m_family != null) {
				m_wins.m_family.logic();
			}
			if (m_wins.m_assistant != null) {
				m_wins.m_assistant.logic();
			}
			if (m_wins.m_fuben != null) {
				m_wins.m_fuben.logic();
			}
			if (m_wins.m_neidan != null) {
				m_wins.m_neidan.logic();
			}
			if (m_wins.m_facarbon != null) {
				m_wins.m_facarbon.logic();
			}
			if (m_wins.m_prize != null) {
				m_wins.m_prize.logic();
			}
			if (m_wins.m_jiming != null) {
				m_wins.m_jiming.logic();
			}
			if (m_wins.m_marriage != null && m_wins.m_social != null) {
				if (m_wins.m_social.m_si_fram != null
						&& DWControlsManager.getInstance().contains(
								m_wins.m_social.m_si_fram))
					m_wins.m_marriage.logic();
			}
			if (m_wins.m_tiangong != null) {
				m_wins.m_tiangong.logic();
			}

			if (m_wins.m_bag != null) {
				m_wins.m_bag.logic();
			}
			if (m_wins.m_tower != null) {
				m_wins.m_tower.logic();
			}
			if (m_wins.m_shura_battle != null) {
				m_wins.m_shura_battle.logic();
			}

			if (m_wins.m_luckdraw != null) {
				m_wins.m_luckdraw.logic();
			}

			if (m_wins.m_bagualu != null) {
				m_wins.m_bagualu.logic();
			}
			
			if (m_wins.m_zhanli != null) {
				m_wins.m_zhanli.logic();
			}
			
			if (m_wins.m_getMoney != null) {
				if (m_wins.m_getMoney.m_gm_frame != null
						&& DWControlsManager.getInstance().contains(m_wins.m_getMoney.m_gm_frame)) {
					m_wins.m_getMoney.logic();
				}
			}
		}
		if (++m_timer > 1000000) {
			m_timer = 0;
		}
	}

	private int m_timer = 0;

	public static UIWindows getInstance()
	{
		return m_wins;
	}

	/**
	 * 得到主界面
	 * 
	 * @return
	 */
	public static UIMain getMain()
	{
		if (m_wins != null) {
			return m_wins.m_main;
		}
		return null;
	}

	public static void removeImage(String path)
	{
		ResHead resHead = new ResHead();
		resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_PNG;
		resHead.m_url = path;
		resHead.m_version = 1;
		ResourceManager.getInstance().removeResource(
				DWGameManager.getInstance().m_resourceStatic, resHead);
	}

	public static Bitmap createImage(String path)
	{
		try {
			ResHead resHead = new ResHead();
			resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_PNG;
			resHead.m_url = path;
			resHead.m_version = 1;
			Bitmap temp = ((DWBitmap) ResourceManager.getInstance()
					.loadResource(DWGameManager.getInstance().m_resourceStatic,
							resHead)).getImage();
			if (temp == null) {
				ResHead tempHead = new ResHead();
				tempHead.m_type = DWSerializableFactory.RESOURCE_TYPE_PNG;
				tempHead.m_url = "/img/newui/anniu_6.gnp";
				tempHead.m_version = 1;
				Tools.debugPrintln("图片路径错误,加载图片失败 : " + path);
				return ((DWBitmap) ResourceManager.getInstance().loadResource(
						DWGameManager.getInstance().m_resourceStatic, tempHead))
						.getImage();
			}
			return temp;
		}
		catch (Exception e) {
			ResHead resHead = new ResHead();
			resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_PNG;
			resHead.m_url = "/img/newui/anniu_6.gnp";
			resHead.m_version = 1;
			Tools.debugPrintln("图片路径错误,加载图片失败 : " + path);
			return ((DWBitmap) ResourceManager.getInstance().loadResource(
					DWGameManager.getInstance().m_resourceStatic, resHead))
					.getImage();
		}
	}

	public static Animation createAnimation(String path)
	{
		ResHead resHead = new ResHead();
		resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_ANIM;
		resHead.m_url = path;
		resHead.m_version = 1;
		return (Animation) ResourceManager.getInstance().loadResource(
				DWGameManager.getInstance().m_resourceStatic, resHead);
	}

}
